var formatMinuteTime = function (data) {
  var s;
  var minutes = parseInt((data % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = parseInt((data % (1000 * 60)) / 1000);
  s =
    (minutes < 10 ? '0' + minutes : minutes) +
    ':' +
    (seconds < 10 ? '0' + seconds : seconds);
  return s;
};

var formatHoureTime = function (data) {
  var s;
  var hours = parseInt((data % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = parseInt((data % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = parseInt((data % (1000 * 60)) / 1000);
  s =
    (hours < 10 ? '0' + hours : hours) +
    ':' +
    (minutes < 10 ? '0' + minutes : minutes) +
    ':' +
    (seconds < 10 ? '0' + seconds : seconds);
  return s;
};

var formatDayTime = function (data) {
  var s;
  var day = parseInt((data % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60 * 24));
  var hours = parseInt((data % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = parseInt((data % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = parseInt((data % (1000 * 60)) / 1000);
  s = day + '天' + hours + '时' + minutes + '分' + seconds + '秒';
  return s;
};

var formatLeftTime = function (date) {
  let num = date / (1000 * 60 * 60 * 24 * 30 * 365);
  if (num > 1) return parseInt(num) + ' years';
  num *= 365;
  if (num > 1) return parseInt(num) + ' mons';
  num *= 30;
  if (num > 1) return parseInt(num) + ' days';
  num *= 24;
  if (num > 1) return parseInt(num) + ' hours';
  num *= 60;
  if (num > 1) return parseInt(num) + ' mins';
  num *= 60;
  if (num > 1) return parseInt(num) + ' secs';
  return 'end';
};

/**
 * 截取字符小数点后指定长度
 * @param {*} a
 * @param {*} num
 * @returns
 */
function subNumerString(a, num) {
  if (a < 0.00000001) {
    a = 0;
  }

  a = a + '';
  var aArr = a.split('.');

  if (aArr.length > 1) {
    a = aArr[0] + '.' + aArr[1].substr(0, num);
  }
  if (a == 0) return 0;

  return Number(a);
}

function compare(key, desc = true) {
  return function (value1, value2) {
    var val1 = value1[key];
    var val2 = value2[key];
    return desc ? val1 - val2 : val2 - val1;
  };
}

module.exports = {
  formatMinuteTime,
  formatHoureTime,
  formatDayTime,
  formatLeftTime,
  subNumerString,
  compare,
};
